﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections;
using System.Windows.Controls.DataVisualization;
using CustomControls;

using System.ComponentModel;
using ActivityTracker.Library;

using Common.Library;

namespace ActivityTracker
{
    public partial class Monthly_TimeSpentPieChart : PhoneApplicationPage
    {
        #region global

        Charts objCharts = new Charts();
        MonthlyChart objMonthlyChart = new MonthlyChart();

        #endregion

        #region constructor

        public Monthly_TimeSpentPieChart()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                string selectedYear = "";
                string selectedMonth = "";
                lpicMonth.ItemsSource = objCharts.GetMonths();
                lpicYear.ItemsSource = objCharts.GetYears();
                if (NavigationContext.QueryString.TryGetValue("SelectedYearindex", out selectedYear) && NavigationContext.QueryString.TryGetValue("SelectedMonthindex", out selectedMonth))
                {
                    lpicMonth.SelectedIndex = Convert.ToInt32(selectedMonth);
                    lpicYear.SelectedIndex = Convert.ToInt32(selectedYear);
                }
                LoadPieChart(lpicMonth.SelectedItem.ToString(), lpicYear.SelectedItem.ToString());
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In Monthly_TimeSpentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnBarChart_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Monthly/Monthly_TimeSpentBarChart.xaml?SelectedYearindex=" + lpicYear.SelectedIndex.ToString() + "&SelectedMonthindex=" + lpicMonth.SelectedIndex.ToString(), UriKind.Relative));
        }
        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Monthly/Monthly_CategoryBarChart.xaml", UriKind.Relative));
        }
        private void btnok_Click(object sender, EventArgs e)
        {
            try
            {
                LoadPieChart(lpicMonth.SelectedItem.ToString(), lpicYear.SelectedItem.ToString());
            }
            catch (Exception ex)
            {
                string mess = "Exception in btnok_Click Method In Monthly_TimeSpentPieChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void labeledPieSeries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.DetailsControl.Content = ((LabeledPieSeries)sender).SelectedItem;
        }

        #endregion

        #region Custom

        private void LoadPieChart(string month, string year)
        {
            try
            {
                List<ChartProp> objChartList = objMonthlyChart.LoadTimeSpentData(month, year, ChartType.Pie);
                if (objChartList.Count > 0)
                    txtblkResult.Visibility = Visibility.Collapsed;
                else
                    txtblkResult.Visibility = Visibility.Visible;
                ContentPanel.DataContext = objChartList;
                PieChart.LegendStyle = ChartLegendStyles.hidelegend();
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in LoadPieChart Method In Monthly_TimeSpentPieChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                ex.Data.Add("month_LoadPieChart", month);
                ex.Data.Add("year_LoadPieChart", year);
                foreach (DictionaryEntry d in ex.Data)
                    exNew.Data.Add(d.Key, d.Value);
                throw exNew;
            }
        }

        #endregion

       
    }
}